import requests
import json
# 定义查询函数
def ip_query(ip):
    url = f"https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip}"
    response = requests.get(url)
    # 判断是否请求成功
    if response.status_code == 200:
        result = json.loads(response.text)
        if result["code"] == 0:
            data = result["data"]
            country = data["country"]
            province = data["region"]
            isp = data["isp"]
            print(f"IP: {ip}\n国家: {country}\n省份: {province}\n运营商: {isp}")
            with open("ip_info.txt", "a", encoding="utf-8") as f:
                f.write(f"{ip},{country},{province},{isp}\n")
        else:
            print("查询失败")
    else:
        print("请求失败")

IP_ADD = input("输入你想要查询的IP地址：")

ip_query(IP_ADD)